//
//  HomeCatesView.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/20.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "HomeCatesView.h"
#import "TPCatesTagModel.h"
@interface HomeCatesView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,weak)UIButton *caseButton;
@property (nonatomic,weak)UIButton *tagButton;
@property (nonatomic,strong)NSMutableArray *catesArray;

@end
@implementation HomeCatesView



-(instancetype)initWithFrame:(CGRect)frame{
    
    if(self = [super initWithFrame:frame]){
        
        _catesArray = [NSMutableArray arrayWithObjects:@[],@[], nil];
        [self setup];
        
    }
    return self;
}
-(void)setup{
    
    self.backgroundColor = TP_COLOR_245;
    self.titleView.backgroundColor = [UIColor whiteColor];
    self.titleView.frame = CGRectMake(0, 8.0, self.width, 38.0);
    UIImageView *line = [self.titleView tp_addLineBottomWithColor:TP_COLOR_240];
    [line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10.0);
        make.right.mas_equalTo(-10.0);
    }];
    
    @weakify(self);
    UIButton *caseButton = [UIButton buttonWithTitle:@"分类" toHeight:30.0 ActionHandler:^(NSInteger tag, UIButton *button) {
        @strongify(self);
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        button.selected = NO;
        self.tagButton.selected = YES;
    }];
    [caseButton setTitleColor:TPBlack_333333 forState:UIControlStateSelected];
    [caseButton setBackgroundImageColor:[UIColor clearColor] forState:UIControlStateSelected];
   
    [self.titleView addSubview:caseButton];
    _caseButton = caseButton;
    [caseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60.0);
        make.centerX.mas_equalTo(0.0).offset(-IPHONE_WIDTH*0.25);
        make.height.mas_equalTo(28.0);
        make.centerY.mas_equalTo(self.titleView.mas_centerY);
    }];
    
    
    UIButton *tagButton = [UIButton buttonWithTitle:@"效果" toHeight:30.0 ActionHandler:^(NSInteger tag, UIButton *button) {
        @strongify(self);
         [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        button.selected = NO;
        self.caseButton.selected = YES;
    }];
     tagButton.selected = YES;
    _tagButton = tagButton;
    [tagButton setTitleColor:TPBlack_333333 forState:UIControlStateSelected];
    [tagButton setBackgroundImageColor:[UIColor clearColor] forState:UIControlStateSelected];
    [self.titleView addSubview:tagButton];
    [tagButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60.0);
        make.centerX.mas_equalTo(0.0).offset(IPHONE_WIDTH*0.25);
        make.height.mas_equalTo(28.0);
        make.centerY.mas_equalTo(self.titleView.mas_centerY);
    }];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.pagingEnabled = YES;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
//    _collectionView.hidden = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[HomeCatesCCell class] forCellWithReuseIdentifier:@"HomeCatesCCell"];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0.0);
        make.bottom.mas_equalTo(-8.0);
        make.top.mas_equalTo(0.0).offset(self.titleView.bottom);
    }];
    [_collectionView layoutIfNeeded];
    
    [self getCatesRequest];
    
}
-(void)getCatesRequest{
    
    
    [[TPRequestClient sharedClient]getTemplateCategory_success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPCatesTagModel class] json:respone.data[@"data"]];
        
        
        if(array){
            [[NSUserDefaults standardUserDefaults]setObject:[array yy_modelToJSONString] forKey:kNSUserDefaultsCatesName];
            [_catesArray replaceObjectAtIndex:0 withObject:array];
        }
        [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        ;
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        ;
    }];
    
    
    [[TPRequestClient sharedClient]getTemplateEffect_success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPCatesTagModel class] json:respone.data[@"data"]];
        if(array){
            [_catesArray replaceObjectAtIndex:1 withObject:array];
            [[NSUserDefaults standardUserDefaults]setObject:[array yy_modelToJSONString] forKey:kNSUserDefaultsTagsName];
            
//            DLog(@"---%@----%@",[[NSUserDefaults standardUserDefaults]objectForKey:kNSUserDefaultsCatesName],[[NSUserDefaults standardUserDefaults]objectForKey:kNSUserDefaultsTagsName]);
        }
        [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        ;
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        ;
    }];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(section == 0)
        return MIN(8, [self.catesArray[section] count]);
    return MIN(12, [self.catesArray[section] count]);;
}
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    HomeCatesCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCatesCCell" forIndexPath:indexPath];
    cell.array = self.catesArray;
    cell.indexPath = indexPath;
    
//    cell.model = nil;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(self.cateBlock){
        self.cateBlock(indexPath, self.catesArray[indexPath.section][indexPath.row]);
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0)
    return CGSizeMake(IPHONE_WIDTH/4, collectionView.height/2);
    return CGSizeMake(IPHONE_WIDTH/4, collectionView.height/3);
}

@end

@interface HomeCatesCCell()

@end

@implementation HomeCatesCCell
@synthesize indexPath = _indexPath;
- (instancetype)init
{
    self = [super init];
    if (self) {
//        [self setup];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self setup];
    }
    return self;
}
-(void)setup{
//     _array = @[@[@"眼部",@"嫩白",@"美白",@"唇部",@"修复",@"塑形",@"瘦脸",@"更多"],@[@"保湿",@"抗衰",@"再生",@"嫩肤",@"减脂",@"美白",@"祛痘",@"减肥",@"提升",@"毛孔",@"紧致",@"净肤"]];
    self.image_view.contentMode = UIViewContentModeScaleAspectFit;
    [self.image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(7.0);
        make.centerX.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(28.0, 28.0));
    }];
//    self.title_label.textAlignment = NSTextAlignmentCenter;
    self.title_label.font = fontSystemOfSize(15);
    [self.title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.image_view.mas_bottom).offset(3.0);
        make.bottom.mas_equalTo(0.0);
        make.centerX.mas_equalTo(self.contentView);
    }];
    self.titleButton.userInteractionEnabled = NO;
    [self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self);
        make.height.mas_equalTo(self.contentView).multipliedBy(0.6);
        make.width.mas_equalTo(self).multipliedBy(0.7);
    }];
    
    
    [self.titleButton setBackgroundImageColor:TPAllBackgroundColor forState:UIControlStateSelected];
    [self.titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    ViewBorderRadius(self.titleButton, (self.height *0.6)/2, 1.0, TPAllBackgroundColor);
    
}
-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
    
    self.image_view.hidden = YES;
    self.title_label.hidden = YES;
    self.titleButton.hidden = YES;
    
    TPCatesTagModel *model = self.array[indexPath.section][indexPath.row];
    [self.titleButton setTitle:model.l_name forState:UIControlStateNormal];
    self.title_label.text = model.gc_name;
    
    [self.image_view ym_setImageWithURL:model.gc_images placeholderImage:ImageNamed(@"更多")];
    if(indexPath.section == 0){
        
        self.image_view.hidden = NO;
        self.title_label.hidden = NO;
        
    }else{
        self.titleButton.hidden = NO;
    }
    if(indexPath.section==0 && indexPath.row == 7){
        self.title_label.text = @"更多";
    }
    if(indexPath.section==1 && indexPath.row == 11){
        [self.titleButton setTitle:@"更多" forState:0];
    }
    
    
     ViewBorderRadius(self.titleButton, (self.height *0.6)/2, 1.0, TPAllBackgroundColor);
}
-(void)setModel:(TPCatesTagModel *)model{
    _model = model;
    NSString *string = model.l_name.length >0 ? model.l_name:model.gc_name;
    [self.titleButton setTitle:string forState:UIControlStateNormal];
}



@end
